package com.example.backend.mapper;


import com.example.backend.entity.Shop;
import com.github.pagehelper.util.StringUtil;

import java.util.HashMap;

public class ShopMapperProvider {
    public String getUpdateShopSql(Shop shop) {
    StringBuffer sql = new StringBuffer("update shop set id = #{id} ");
    if(StringUtil.isNotEmpty(shop.getShopname())){
        sql.append(" ,shopname = #{shopname} ");
    }
    if(StringUtil.isNotEmpty(shop.getPwd())){
        sql.append(" ,pwd = #{pwd} ");
    }
    if(StringUtil.isNotEmpty(shop.getChecked())){
        sql.append(" ,checked = #{checked} ");
    }
    if(StringUtil.isNotEmpty(shop.getScore())){
        sql.append(" ,score = #{score} ");
    }
    if(StringUtil.isNotEmpty(shop.getPhone())){
        sql.append(" ,phone = #{phone} ");
    }
    if(StringUtil.isNotEmpty(shop.getAddress())){
        sql.append(" ,address = #{address} ");
    }
    if(StringUtil.isNotEmpty(shop.getState())){
        sql.append(" ,state = #{state} ");
    }
    if(StringUtil.isNotEmpty(shop.getPicture())){
        sql.append(" ,picture = #{picture} ");
    }
    if(StringUtil.isNotEmpty(shop.getIncome())){
        sql.append(" ,income = #{income} ");
    }
    if(shop.getGratuity()!=null&&shop.getGratuity()!= 0){
        sql.append(" ,gratuity = #{gratuity} ");
    }

    sql.append(" where id = #{id}");
    return sql.toString();
}

    /**
     * 获取查询用户的SQL
     * @param shop
     * @return sql
     */
    public String getSelectShopsSql(Shop shop) {
        StringBuffer sql = new StringBuffer("select * from shop where 1=1 ");
        if (shop.getShopname() != null && !shop.getShopname().isEmpty()) {
            sql.append(" and shopname like concat('%', #{shopname}, '%') ");
        }
        if (shop.getAddress() != null && !shop.getAddress().isEmpty()) {
            sql.append(" and address like concat('%', #{address}, '%') ");
        }
        if (shop.getScore() != null && !shop.getScore().isEmpty()) {
            sql.append(" and score like concat('%', #{score}, '%') ");
        }
        if (shop.getChecked() != null && !shop.getChecked().isEmpty()) {
            sql.append(" and checked = #{checked} ");
        }
        if (shop.getState() != null && !shop.getState().isEmpty()) {
            sql.append(" and state = #{state} ");
        }
        if (shop.getPhone() != null && !shop.getPhone().isEmpty()) {
            sql.append(" and phone like concat('%', #{phone}, '%') ");
        }
        if (shop.getGratuity() != null && shop.getGratuity() != 0) {
            sql.append(" and gratuity = #{gratuity} ");
        }
        return sql.toString();
    }


    public String getSelectShopsByUser2Sql(HashMap<String,Shop> map){

        Shop shop1 = map.get("param1");
        Shop shop2 = map.get("param2");
        return "";
    }

    public String getUpdateShopScoreSql(Integer id) {
        return "update shop set score = (select avg(score) from score where bescorer_id = #{id} and bescorer_type = 1) where id = #{id}";
    }
}
